package com.yunji.news.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yunji.news.common.Result;
import com.yunji.news.entity.ExamOutline;
import com.yunji.news.service.ExamOutlineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 校测考纲控制器
 *
 * @author yunji
 * @since 2024-11-26
 */
@RestController
@RequestMapping("/exam/outlines")
@CrossOrigin
public class ExamOutlineController {

    @Autowired
    private ExamOutlineService outlineService;

    /**
     * 分页查询校测考纲（管理端）
     */
    @GetMapping("/admin")
    public Result<IPage<ExamOutline>> getOutlines(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size,
            @RequestParam(required = false) Long schoolId,
            @RequestParam(required = false) Long majorId,
            @RequestParam(required = false) Integer examYear,
            @RequestParam(required = false) String examType,
            @RequestParam(required = false) String keyword) {
        
        IPage<ExamOutline> result = outlineService.getOutlinePage(
            page, size, schoolId, majorId, examYear, examType, keyword);
        return Result.success(result);
    }

    /**
     * 获取已发布的考纲列表（前端展示）
     */
    @GetMapping("/public")
    public Result<List<ExamOutline>> getPublishedOutlines(
            @RequestParam(required = false) Long schoolId,
            @RequestParam(required = false) Long majorId,
            @RequestParam(required = false) Integer examYear) {
        
        List<ExamOutline> outlines = outlineService.getPublishedOutlines(schoolId, majorId, examYear);
        return Result.success(outlines);
    }

    /**
     * 获取热门考纲
     */
    @GetMapping("/popular")
    public Result<List<ExamOutline>> getPopularOutlines(
            @RequestParam(defaultValue = "10") Integer limit) {
        
        List<ExamOutline> outlines = outlineService.getPopularOutlines(limit);
        return Result.success(outlines);
    }

    /**
     * 获取考纲详情
     */
    @GetMapping("/{id}")
    public Result<ExamOutline> getOutline(@PathVariable Long id) {
        ExamOutline outline = outlineService.getById(id);
        if (outline == null) {
            return Result.error("考纲不存在");
        }
        return Result.success(outline);
    }

    /**
     * 创建考纲
     */
    @PostMapping
    public Result<String> createOutline(@RequestBody ExamOutline outline) {
        try {
            boolean success = outlineService.createOutline(outline);
            if (success) {
                return Result.success("考纲创建成功");
            } else {
                return Result.error("考纲创建失败");
            }
        } catch (Exception e) {
            return Result.error("考纲创建失败：" + e.getMessage());
        }
    }

    /**
     * 更新考纲
     */
    @PutMapping("/{id}")
    public Result<String> updateOutline(@PathVariable Long id, @RequestBody ExamOutline outline) {
        outline.setId(id);
        boolean success = outlineService.updateOutline(outline);
        if (success) {
            return Result.success("考纲更新成功");
        } else {
            return Result.error("考纲更新失败");
        }
    }

    /**
     * 发布考纲
     */
    @PutMapping("/{id}/publish")
    public Result<String> publishOutline(@PathVariable Long id) {
        boolean success = outlineService.publishOutline(id);
        if (success) {
            return Result.success("考纲发布成功");
        } else {
            return Result.error("考纲发布失败");
        }
    }

    /**
     * 删除考纲
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteOutline(@PathVariable Long id) {
        boolean success = outlineService.removeById(id);
        if (success) {
            return Result.success("考纲删除成功");
        } else {
            return Result.error("考纲删除失败");
        }
    }

    /**
     * 记录下载
     */
    @PostMapping("/{id}/download")
    public Result<String> recordDownload(@PathVariable Long id) {
        boolean success = outlineService.recordDownload(id);
        if (success) {
            return Result.success("下载记录成功");
        } else {
            return Result.error("下载记录失败");
        }
    }
}